package cn.iocoder.yudao.module.shop.service.itemtimesrecharge;

import cn.iocoder.yudao.module.shop.controller.admin.itemtimes.vo.ShopItemTimesCreateReqVO;
import cn.iocoder.yudao.module.shop.dal.dataobject.item.ShopItemDO;
import cn.iocoder.yudao.module.shop.service.item.ShopItemService;
import cn.iocoder.yudao.module.shop.service.itemtimes.ShopItemTimesService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.itemtimesrecharge.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemtimesrecharge.ShopItemTimesRechargeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.itemtimesrecharge.ShopItemTimesRechargeConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.itemtimesrecharge.ShopItemTimesRechargeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 项目次数充值 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopItemTimesRechargeServiceImpl implements ShopItemTimesRechargeService {

    @Resource
    private ShopItemTimesRechargeMapper itemTimesRechargeMapper;

    @Resource
    private ShopItemService shopItemService;

    @Resource
    private ShopItemTimesService shopItemTimesService;

    @Override
    public Long createItemTimesRecharge(ShopItemTimesRechargeCreateReqVO createReqVO) {
        // 插入
        ShopItemTimesRechargeDO itemTimesRecharge = ShopItemTimesRechargeConvert.INSTANCE.convert(createReqVO);
        itemTimesRechargeMapper.insert(itemTimesRecharge);
        // 返回
        return itemTimesRecharge.getId();
    }

    @Override
    public Long timesRecharge(Long memberId, Long itemId, Integer payPrice, Integer times, String reason, Long tenantId){
        ShopItemDO shopItemDO = shopItemService.getItem(itemId);
        if(shopItemDO == null){
            throw exception(ITEM_NOT_EXISTS);
        }

        ShopItemTimesRechargeCreateReqVO reqVO = new ShopItemTimesRechargeCreateReqVO();
        reqVO.setMemberId(memberId);
        reqVO.setItemId(itemId);
        reqVO.setItemName(shopItemDO.getName());
        reqVO.setPayPrice(payPrice);
        reqVO.setTimes(times);
        reqVO.setReason(reason);
        reqVO.setPayTime(LocalDateTime.now());
        reqVO.setTenantId(tenantId);

        Long id = createItemTimesRecharge(reqVO);

        shopItemTimesService.addTimes(memberId, itemId, times, reason, payPrice, tenantId);
        return id;
    }

    @Override
    public void updateItemTimesRecharge(ShopItemTimesRechargeUpdateReqVO updateReqVO) {
        // 校验存在
        validateItemTimesRechargeExists(updateReqVO.getId());
        // 更新
        ShopItemTimesRechargeDO updateObj = ShopItemTimesRechargeConvert.INSTANCE.convert(updateReqVO);
        itemTimesRechargeMapper.updateById(updateObj);
    }

    @Override
    public void deleteItemTimesRecharge(Long id) {
        // 校验存在
        validateItemTimesRechargeExists(id);
        // 删除
        itemTimesRechargeMapper.deleteById(id);
    }

    private void validateItemTimesRechargeExists(Long id) {
        if (itemTimesRechargeMapper.selectById(id) == null) {
            throw exception(ITEM_TIMES_RECHARGE_NOT_EXISTS);
        }
    }

    @Override
    public ShopItemTimesRechargeDO getItemTimesRecharge(Long id) {
        return itemTimesRechargeMapper.selectById(id);
    }

    @Override
    public List<ShopItemTimesRechargeDO> getItemTimesRechargeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return itemTimesRechargeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopItemTimesRechargeDO> getItemTimesRechargePage(ShopItemTimesRechargePageReqVO pageReqVO) {
        return itemTimesRechargeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopItemTimesRechargeDO> getItemTimesRechargeList(ShopItemTimesRechargeExportReqVO exportReqVO) {
        return itemTimesRechargeMapper.selectList(exportReqVO);
    }

    @Override
    public Long getChargeCount(Long memberId){
        return itemTimesRechargeMapper.selectCount(memberId);
    }

}
